package com.explorex.infra.puma.gateway.netty.ws;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;
import lombok.extern.slf4j.Slf4j;

import java.io.InputStream;
import java.util.Map;

/**
 * @author: JacceYang
 * @createTime: 2022/07/14 9:19 下午
 * @description: HttpResourceFile for file format handle
 */
@Slf4j
public class HttpResourceFile extends BaseHttpResource {

    @Override
    protected ByteBuf buildRes(String resPath, Map<String, Object> parameters) {
        try {
            InputStream inputStream = HttpResourceFile.class.getResourceAsStream("/" + this.getRootDir() + resPath);
            if (inputStream != null) {
                int available = inputStream.available();
                if (available != 0) {
                    byte[] bytes = new byte[available];
                    inputStream.read(bytes);
                    return ByteBufAllocator.DEFAULT.buffer(bytes.length).writeBytes(bytes);
                }
            }
        } catch (Exception e) {
            log.error("HttpResourceFile called exception resPath={},parameters={}", resPath, parameters,e);
        }
        return null;
    }
}
